package com.nil.codegen.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nil.codegen.bean.FileStoreInfo;
import com.nil.codegen.domain.entity.FileModel;
import com.nil.codegen.mapper.FileManageMapper;
import com.nil.codegen.service.IFileManageService;
import com.nil.codegen.utils.DateUtil;
import com.nil.codegen.utils.FileUtil;
import com.nil.codegen.utils.MinIOUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;

@Slf4j
@Service
public class FileManageServiceImpl extends ServiceImpl<FileManageMapper, FileModel> implements IFileManageService {


    @Resource
    private MinIOUtil minioUtil;


    @Override
    public FileModel uploadFile(MultipartFile file) {
        log.info("==================== 开始上传文件 ====================");
        String filename = file.getOriginalFilename();
        FileStoreInfo fileStoreInfo = minioUtil.uploadFile(file);

        FileModel fileModel = new FileModel();
        fileModel.setId(DateUtil.getUUID());
        fileModel.setFileName(filename);
        fileModel.setFileSize(FileUtil.getFileSize2KB(file));
        fileModel.setUploadTime(LocalDateTime.now());
        fileModel.setDownloadCount(0);
        fileModel.setDownloadUrl(fileStoreInfo.getUrl());
        this.save(fileModel);
        return fileModel;
    }

    @Override
    public void download(HttpServletResponse response, String fileId) {

    }


}
